package com.controller;

import com.constant.Constant;
import com.entity.User;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.Session;
import com.service.impl.GoldPointServiceImpl;
import com.util.AjaxResult;
import com.util.StringUtil;
import com.vo.GoldPointsVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
@RequestMapping("/gp")

public class GoldPointCtl {
    @Autowired
    GoldPointServiceImpl goldPointServiceImpl;

    @ResponseBody
    @RequestMapping("/findAllRecords")
//    查询所有的积分记录
    public AjaxResult findAllRecords(HttpServletRequest request, String pageNo) {
        System.out.println("qaq");
        User user  = (User) request.getSession().getAttribute(Constant.SESSION_USER);
        int id = user.getId();
        int pageno = StringUtil.getInt(pageNo);

        AjaxResult result = new AjaxResult(true, "查询成功", null);
        try {
            PageInfo<GoldPointsVo> info = goldPointServiceImpl.findAllRecords(pageno,id);
            result.setObj(info);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg(e.getMessage());
        }

        return result;


    }

    //查询剩余金币和积分
    @RequestMapping("/front_record.do")
    public String findGoldPointCount(HttpServletRequest request, HttpServletResponse response) {
        User user = (User) request.getSession().getAttribute(Constant.SESSION_USER);
        GoldPointsVo userGpInfo = goldPointServiceImpl.findGoldPointCount(user.getId());
//        把当前用户的积分金币对象传入session中，用于金币兑换用
        request.getSession().setAttribute("userGpInfo", userGpInfo);


        return "/front/front_record";


    }

    //    积分兑换金币   pj

    @RequestMapping("/exchangePoints")
    public String exchangePoints(String pointscount, HttpServletRequest request, HttpServletResponse response) {
        GoldPointsVo goldPointsVo = (GoldPointsVo) request.getSession().getAttribute("userGpInfo");
        Integer pointsCt = Integer.valueOf(pointscount);
        User user  = (User) request.getSession().getAttribute(Constant.SESSION_USER);
        int id = user.getId();
//        GoldPointsVo userGpInfo=goldPointServiceImpl.findGoldPointCount(user.getId());

        if (goldPointsVo.getSum_point_count() < 10 || goldPointsVo.getSum_point_count() < pointsCt) {
            request.getSession().setAttribute("pointsmsg", "积分不足");
        } else {
            int i = goldPointServiceImpl.exchangePoints(id, pointsCt);
            if (i == 1) {
                System.out.println("添加成功");
            } else {
                request.getSession().setAttribute("pointsmsg", "添加失败");
            }
        }
//        return "redirect:front_record.do";
        return "/front/front_record.do";
    }

//前台用户中我的积分显示 pj

    @RequestMapping("/findFrontSumByUid.do")
    @ResponseBody
       public AjaxResult findFrontSumByUid(HttpServletRequest request){
        User user  = (User) request.getSession().getAttribute(Constant.SESSION_USER);
        AjaxResult frontSumByUid = goldPointServiceImpl.findFrontSumByUid(user.getId());
        return frontSumByUid;
    }



}


